<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="isShow" @close="handleCloseDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in management"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button
        type="primary"
        size="small"
        @click="clickSubmit"
      >确定</el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetSimple } from '@/api/employees'
import { reqAddDepartments, reqGetDepartAll, reqUpdateDepartments } from '@/api/department'
export default {
  name: 'Adddepts',
  props: {
    // 需要父元素进行显隐控制
    isShow: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    deptList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 列表数据转树形数据验证name
    const validateName = (rules, value, callback) => {
      // 用pid找寻相同的nodeData.id的数据 获取同级目录
      console.log(value)

      // 用同一种校验会有冲突 进行检查判断
      // 如果是编辑操作是可以重复的
      if (this.nodeData.name === value) {
        callback()
      }
      const res = this.deptList.filter((item) => item.pid === this.nodeData.id)
      // 输入内容与发送的数据是否相同
      const result = res.some((item) => item.name === value)
      console.log(result)
      result
        ? callback(new Error(`同级部门已经出现${value}相同部门`))
        : callback()
    }
    // 验证code
    const validateCode = (rules, value, callback) => {
      if (this.form.id && this.nodeData.code === value) {
        callback()
      }
      const res = this.deptList.some((item) => item.code === value)
      // console.log(res)
      res ? callback(new Error(`同级已${value}有相同编号`)) : callback()
    }
    return {
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '部门内容不能为空',
            trigger: ['blur', 'change']
          },
          { min: 1, max: 50, message: '填写内容1-50个字符', trigger: ['blur'] },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          {
            required: true,
            message: '字符内容不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 50,
            message: '填写内容1-50个字符',
            trigger: ['blur']
          },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: ['blur', 'change']
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 300,
            message: '部门介绍要求1-300个字符',
            trigger: ['blur', 'change']
          }
        ]
      },
      // 部门负责人暂存
      management: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getEmployees()
  },

  mounted() {},
  methods: {
    // 提交dia数据
    clickSubmit() {
      this.$refs.form.validate(async(flag) => {
        if (!flag) return
        console.log('已经通过！！')
        if (this.form.id) { // 根据验证有无id 判断是否为编辑与新增
          // 更新修改数据
          const { message, success } = await reqUpdateDepartments(this.form)
          if (!success) return this.$message.error(message)
          this.$message.success(message)
        } else {
          // 新增数据内容
          const res = await reqAddDepartments({
            ...this.form,
            pid: this.nodeData.id
          })
          console.log(res)
          this.$message.success('添加部门成功')
        }

        this.$emit('add-dept')
        this.$refs.form.resetFields() // 重置状态
      })
    },
    // 关闭Dialog
    handleCloseDialog() {
      this.$emit('handleClose', false)
      this.$refs.form.resetFields() // 重置状态
      // 清除数据回放的缓存id
      this.form = {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
    },
    // 获取员工简单数据列表
    async getEmployees() {
      const { data } = await reqGetSimple()
      this.management = data
      console.log(data)
    },
    // 获取企业详情
    async getGetDepartDetail() {
      const { success, message, data } = await reqGetDepartAll(
        this.nodeData.id
      )
      if (!success) return
      this.$message.success(message)
      this.form = data
    }
  }
}
</script>

<style scoped>
</style>
